#include "../include/buffer/buffer.hpp"
#include <fstream>
#include <iostream>

// 从文件中读取数据(文件数据读取到缓冲区，缓冲区写入到文件)

int main()
{
    std::ifstream input_stream("./logfile/test.log", std::ios::binary);
    if (input_stream.is_open() == false)
        return -1;
    input_stream.seekg(0, std::ios::end); // 读写位置跳转到文件末尾
    size_t file_size = input_stream.tellg(); // 获取读写位置相对于起始位置的偏移量
    std::cout << file_size << std::endl;
    input_stream.seekg(0, std::ios::beg); // 读写位置跳转到文件起始位置
    std::string payload;
    payload.resize(file_size);
    input_stream.read(&payload[0], file_size);
    if (input_stream.good() == false)
    {
        std::cout << "read failed" << std::endl;
        return -1;
    }
    log_system::Buffer buffer;
    for(int i = 0; i < payload.size(); ++i)
    {
        buffer.write(&payload[i], 1);
    }
    std::cout << buffer.readableLength() << std::endl;

    std::ofstream output_stream("./logfile/tmp.log", std::ios::binary);
    // output_stream.write(buffer.readableBeginPointer(), buffer.readableLength());  // 一次性写入

    size_t readableLength = buffer.readableLength();
    for(int i = 0; i < readableLength; ++i)  // readalbeLength是逐渐减小的(按照字节写入)
    {
        output_stream.write(buffer.readableBeginPointer(), 1);
        if(output_stream.good() == false )
        {
            std::cout << "write failed" << std::endl;
            return -1;
        }
        buffer.offsetReadPonter(1);
    }

    output_stream.close();

    return 0;
}
